/*
 * 表格中td 操作，键盘上下左右键控制td焦点。
 */
//var  formId= "triage_form_info";
//var  classN="info-group-item";
var  keycontrol="keycontrol";
/*最外层父级：top-key-control*/
//$(function() {
//	// 添加分诊键盘事件 form的id 和控制的类名,以及input等输入选择框的类名
//	controlFromEvent();
//})
document.onkeydown = function(event) {
	var e = event || window.event || arguments.callee.caller.arguments[0];
	if (e.which === 39) // 向右的时候，让form 的第一个input聚焦
	{
		if ($(".drop_list").is(':hidden')) {

			var fist_E = $("#" + formId).find("." + classN).find("." + keycontrol).first();
			fist_E.focus();
			fist_E.select();
		}
	} else if(e && e.keyCode === 40){ // 按向下箭头
		if($("#drop_doctor_list").is(":visible")){//医生信息
  		  var selectedId = $("#grid_doctor_list").jqGrid("getGridParam", "selrow");
  		  $("#grid_doctor_list").jqGrid("setSelection", ++selectedId);
		}
		 
	 }else if(e && e.keyCode === 38){ //向上箭头
		 if($("#drop_doctor_list").is(":visible")){//医生信息
	  		  var selectedId = $("#grid_doctor_list").jqGrid("getGridParam", "selrow");
	  		  $("#grid_doctor_list").jqGrid("setSelection", --selectedId);
			}
	 }
	 else if(e && e.keyCode === 13){ //回车事件
  	  if($("#drop_doctor_list").is(":visible") ){
	  		var rowid = $("#grid_doctor_list").jqGrid("getGridParam", "selrow");
			$("#"+rowid,'#grid_doctor_list').trigger("dblclick");
  		  }
  	  }

}

var fangxiang;
var formEle;
// form控制所有的需要加上keycontrol类；
function controlFromEvent(formId,classN) {
	 formEle = $("#" + formId).find("." + classN);
	formEle.each(function(i,ele){
		$(ele).keydown(function(e) {
					if (e.which === 13) // ---->向右键和回车，键盘事件往下走
					{
						debugger;
						var next = findNextEle($(ele),"n");
						fangxiang = "you";
						beforeAndAfter(next);
					} else if (e.which === 37) {// <----向左键
						var next = $(this).prev();
						fangxiang = "zuo";
						beforeAndAfter(next);
					} else if (e.which === 38) {
						// if($(".drop_list").is(':hidden')){
						//				
						// var className = $(this).find("textarea").attr("class");
						// $(this).parents("tr").prev().find("."+className).focus();
						// }
					}
					else if (e.which === 40) // 向下的时候，让form 的第一个input聚焦
					{
						if ($(".drop_list").is(':hidden')&&(".select2-container--open").length<1) {

							var fist_E = $("#" + formId).find("." + classN).find("." + keycontrol);
							fist_E.focus();
							fist_E.select();
							;
						}
					}
				})
	})
}

function beforeAndAfter(next) {
	var flag;
	var next2; // 最终focus 的元素
	var curE = next.find("." + keycontrol);
	if (curE.length >0) {
		if (next.is(':visible')) { // 判断li是不是显示的，如果是显示的，则键盘控制
			flag = curE[0].nodeName; // 获取input等类型
			if(curE[0].type == "radio"){
				next2 = findNextEle(next,"n");
				beforeAndAfter(next2);
			}else{
				if (flag == "INPUT" || flag == "TEXTAREA") {
					var isReadOnly = next.find(".keycontrol").attr("readonly");
					var isdisable = next.find(".keycontrol").attr("disabled");
					if (isReadOnly||isdisable) {
						if (fangxiang == "you") {
							next2 = findNextEle(next,"n");
//					next2 = next2.next();
						} else if (fangxiang == "zuo") {
							next2 =  findNextEle(next,"p");
						}
						beforeAndAfter(next2);
						// dropDoctorList();
					} else {
						
						curE.focus();
						curE.select();
					}
					
				} else if (flag == "SELECT") {
					curE.select2("open");
					select2Event(next);
				} else if (flag == "BUTTON") {
					$(curE).focus();
					$(curE).trigger("click");
				}
			}
		}
	} else {
		next2 =  findNextEle(next2);
		beforeAndAfter(next2);
	}
}
function select2Event(next) {
	next.find("select").on("select2:close", function() {
		debugger;
		var next1;
		if (fangxiang == "you") {
			next1 =  findNextEle(next,"n");
		} else if (fangxiang == "zuo") {
			next1 = findNextEle(next,"p");
		}
		if (next1.length > 0) {
			beforeAndAfter(next1);

		}
	})
}

function findNextEle(ele,type){
	var next;
	if(type =="n"){
		
		var nextParent = $(ele).next(".key_control_div");
	}else{
		
		var nextParent = $(ele).prev(".key_control_div");
	}
	if(nextParent.length > 0 && nextParent.is(":visible") ){
		next = nextParent;
		
	}else if(nextParent.length > 0 && nextParent.is(":hidden") ){
		return findNextEle(nextParent,type);
	}
	else if(nextParent.length<1){
		if(type =="n"){
			var nextParRow =  $(ele).parents(".top-key-control").next(".top-key-control");
			if(nextParRow.length > 0  && nextParRow.is(":visible") ){
				next = nextParRow.find(".key_control_div:eq(0)");
			}else{
				return findNextEle(nextParRow.find(".key_control_div:eq(0)"),type);
			}
			
			
		}else{
			
			var nextParRow =  $(ele).parents(".top-key-control").prev(".top-key-control");
			if(nextParRow.length > 0){
				next = nextParRow.find(".key_control_div:eq(0)");
			}
		}
	}
	return next;
	
}







